<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>应急图书管理系统 - 首页</title>
    <style>
        body {
            font-family: "Helvetica Neue", sans-serif;
            margin: 0;
            background-color: #f9f9f9;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .navbar {
            background-color: #2c3e50;
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .navbar-brand {
            font-size: 24px;
            font-weight: bold;
            text-decoration: none;
            color: white;
        }

        .navbar-menu {
            display: flex;
            gap: 20px;
        }

        .navbar-menu a {
            color: white;
            text-decoration: none;
            font-size: 16px;
            padding: 8px 12px;
            border-radius: 4px;
            transition: background-color 0.3s ease;
        }

        .navbar-menu a:hover {
            background-color: #34495e;
        }

        .navbar-auth {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .navbar-auth span {
            font-size: 16px;
        }

        .auth-btn {
            padding: 8px 16px;
            font-size: 14px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background-color: #2196F3;
            color: white;
            transition: background-color 0.3s ease;
            text-decoration: none;
        }

        .auth-btn:hover {
            background-color: #1976D2;
        }

        .reminder-icon {
            cursor: pointer;
            font-size: 18px;
            color: #FF9800;
            padding: 8px;
            transition: color 0.3s ease;
        }

        .reminder-icon:hover {
            color: #e68a00;
        }

        .reminder-popup {
            display: none;
            position: fixed;
            top: 100px;
            right: 20px;
            width: 300px;
            max-height: 400px;
            background-color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            border-radius: 8px;
            overflow-y: auto;
            z-index: 1000;
        }

        .reminder-popup.show {
            display: block;
        }

        .reminder-header {
            background-color: #2c3e50;
            color: white;
            padding: 10px;
            font-size: 16px;
            font-weight: bold;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .reminder-close {
            cursor: pointer;
            font-size: 14px;
            color: #FF9800;
        }

        .reminder-close:hover {
            color: #e68a00;
        }

        .reminder-content {
            padding: 10px;
        }

        .reminder-item {
            border-bottom: 1px solid #ddd;
            padding: 8px 0;
            font-size: 14px;
        }

        .reminder-item:last-child {
            border-bottom: none;
        }

        .reminder-item span {
            font-weight: bold;
        }

        .content {
            flex: 1;
            padding: 30px;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        form {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
            justify-content: center;
        }

        label {
            font-size: 14px;
        }

        input[type="text"],
        input[type="number"],
        input[type="datetime-local"] {
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            padding: 6px 14px;
            font-size: 14px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background-color: #2c3e50;
            color: white;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #1a2733;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        th, td {
            padding: 10px 12px;
            text-align: center;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f0f0f0;
            color: #333;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .action-btn {
            background-color: #2196F3;
            margin: 2px;
            padding: 6px 12px;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .action-btn:hover {
            background-color: #1976D2;
        }

        .action-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        .footer {
            background-color: #2c3e50;
            color: white;
            text-align: center;
            padding: 15px;
            font-size: 14px;
        }

        .footer a {
            color: #2196F3;
            text-decoration: none;
        }

        .footer a:hover {
            text-decoration: underline;
        }
    </style>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const userId = localStorage.getItem('userId');
            if (userId) {
                const link = document.getElementById('myBorrowingsLink');
                link.href = '/borrowing/student/borrowings?userId=' + encodeURIComponent(userId);
            }
        });

        function borrowBook(id) {
            window.location.href = `/borrowing/student/borrow/${id}`;
        }

        function viewDetails(id) {
            window.location.href = `/book/public/book/${id}`;
        }

        function logout() {
            localStorage.removeItem('token');
            localStorage.removeItem('userType');
            localStorage.removeItem('userId');
            localStorage.removeItem('username');
            window.location.href = '/user/login';
        }

        window.onload = function () {
            const token = localStorage.getItem('token');
            const username = localStorage.getItem('username');
            const authSection = document.getElementById('authSection');
            if (token && username) {
                authSection.innerHTML = `
                    <span>欢迎，用户：${username}</span>
                    <a href="/user/login" class="auth-btn" onclick="logout()">退出</a>
                `;
            } else {
                authSection.innerHTML = `
                    <a href="/user/login" class="auth-btn">登录</a>
                `;
                window.location.href = '/user/login';
            }
        };

        function toggleReminderPopup() {
            const popup = document.getElementById('reminderPopup');
            if (popup.classList.contains('show')) {
                popup.classList.remove('show');
            } else {
                generateReminderContent();
                popup.classList.add('show');
            }
        }

        function generateReminderContent() {
            const reminders = generateMockReminders();
            const content = document.getElementById('reminderContent');
            content.innerHTML = '';
            reminders.forEach(reminder => {
                const item = document.createElement('div');
                item.className = 'reminder-item';
                item.innerHTML = `
                    <p><span>ID:</span> ${reminder.id}</p>
                    <p><span>书名:</span> ${reminder.bookTitle}</p>
                    <p><span>借阅人:</span> ${reminder.username}</p>
                    <p><span>应还时间:</span> ${reminder.dueTime}</p>
                    <p><span>状态:</span> ${reminder.status === 'BORROWED' ? '借阅中' : '逾期'}</p>
                `;
                content.appendChild(item);
            });
        }

        function generateMockReminders() {
            const bookTitles = ['《Java编程思想》', '《算法导论》', '《深入理解计算机系统》', '《Python编程》', '《设计模式》'];
            const usernames = ['张三', '李四', '王五', '赵六', '陈七'];
            const statuses = ['BORROWED', 'OVERDUE'];
            const reminders = [];
            const count = Math.floor(Math.random() * 3) + 3; // 随机3-5条

            for (let i = 0; i < count; i++) {
                const id = Math.floor(Math.random() * 1000) + 1;
                const bookTitle = bookTitles[Math.floor(Math.random() * bookTitles.length)];
                const username = usernames[Math.floor(Math.random() * usernames.length)];
                const status = statuses[Math.floor(Math.random() * statuses.length)];
                const dueTime = generateRandomDueTime();
                reminders.push({
                    id: id,
                    bookTitle: bookTitle,
                    username: username,
                    dueTime: dueTime,
                    status: status
                });
            }
            return reminders;
        }

        function generateRandomDueTime() {
            const start = new Date('2025-05-01T00:00:00');
            const end = new Date('2025-07-01T23:59:59');
            const randomDate = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
            return randomDate.toISOString().slice(0, 16).replace('T', ' ');
        }

        function closeReminderPopup() {
            document.getElementById('reminderPopup').classList.remove('show');
        }

        // 点击浮窗外部关闭
        document.addEventListener('click', function (event) {
            const popup = document.getElementById('reminderPopup');
            const icon = document.querySelector('.reminder-icon');
            if (!popup.contains(event.target) && !icon.contains(event.target)) {
                popup.classList.remove('show');
            }
        });
    </script>
</head>
<body>
<header class="navbar">
    <a href="/" class="navbar-brand">应急图书管理系统</a>
    <div class="navbar-menu">
        <a href="/toEmergencyInfoPage">科普</a>
        <a href="/book/student/books">图书列表</a>
        <a id="myBorrowingsLink" href="#">我的借阅</a>
    </div>
    <div class="navbar-auth" id="authSection">
        <!-- 动态生成登录/退出按钮和提醒图标 -->
    </div>
</header>

<main class="content">
    <h1>图书列表</h1>

    <form action="/book/student/books?pageSize=100" method="get">
        <label>书名: <input type="text" name="title" th:value="${param.title}"></label>
        <label>ISBN: <input type="text" name="isbn" th:value="${param.isbn}"></label>
        <button type="submit">查询</button>
    </form>

    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>书名</th>
            <th>作者</th>
            <th>出版社</th>
            <th>ISBN</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="book : ${books}">
            <td th:text="${book.id}"></td>
            <td th:text="${book.title}"></td>
            <td th:text="${book.author}"></td>
            <td th:text="${book.publisher} ?: '无'"></td>
            <td th:text="${book.isbn}"></td>
            <td th:text="${book.status == 'AVAILABLE' ? '可借' : '已借'}"></td>
            <td>
                <button class="action-btn" type="button" th:onclick="'viewDetails(' + ${book.id} + ')'">详情</button>
                <button class="action-btn" type="button" th:onclick="'borrowBook(' + ${book.id} + ')'"
                        th:disabled="${book.status != 'AVAILABLE'}" th:style="${book.status != 'AVAILABLE'} ? 'opacity: 0.5; cursor: not-allowed;' : ''">借阅</button>
            </td>
        </tr>
        <tr th:if="${#lists.isEmpty(books)}">
            <td colspan="7">暂无数据</td>
        </tr>
        </tbody>
    </table>

    <div id="reminderPopup" class="reminder-popup">
        <div class="reminder-header">
            <span>归还提醒</span>
            <span class="reminder-close" onclick="closeReminderPopup()">关闭</span>
        </div>
        <div id="reminderContent" class="reminder-content"></div>
    </div>
</main>

<footer class="footer">
    <p>© 2025 应急图书管理系统 | 联系我们: <a href="mailto:support@bookmanagement.com">support@bookmanagement.com"></a></p>
</footer>
</body>
</html>